package com.demo.hotel_book.provide;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
public class EmailProvide {
    @Autowired
    JavaMailSenderImpl javaMailSender;

    public static String getRandomString() {
        char chars[] = "1234567890QWERTYUIOPASDFGHJKLZXCVBNM".toCharArray();
        Random random = new Random();
        StringBuffer buffer = new StringBuffer();
        int index;   //获取随机chars下标
        for (int i = 0; i < 4; i++) {
            index = random.nextInt(chars.length);  //获取随机chars下标
            buffer.append(chars[index]);
        }
        return buffer.toString();
    }

    public String sendTo(String em) {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        String mima = getRandomString();
        mailMessage.setSubject("通知");
        mailMessage.setText("我们的验证码：" + mima);
        mailMessage.setTo(em);
        mailMessage.setFrom("1254852378@qq.com");
        javaMailSender.send(mailMessage);
        return mima;
    }
}

